// 基础类型扩展
export default function() {

    /**
     * Promise 忽略错误
     * */
    Promise.prototype.ignoreError = function() {
        return this.catch(() => {
            console.log('[Promise] IgnoreError')
        })
    }

    /**
     * Promise 忽略错误并终止执行
     * */
    Promise.prototype.ignoreErrorAndAbort = function():Promise<void> {
        return this.catch(() => {
            console.log('[Promise] IgnoreErrorAndAbort')
            return new Promise(() => {
            })
        })
    }

    /**
     * Date 日期格式化 yyyy-MM-dd
     */
    Date.prototype.toDateString = function() {

        // 验证入参是否有效
        if (this === null || this === undefined)
            return ''

        const month:string = (this.getMonth() + 1).toString().padStart(2, '0')
        const strDate:string = this.getDate().toString().padStart(2, '0')
        const result:string = `${this.getFullYear()}-${month}-${strDate}`
        return result
    }

    /**
     * Date 时间格式化 yyyy-MM-dd HH:mm:ss
     */
    Date.prototype.toDateTimeString = function() {

        // 验证入参是否有效
        if (this === null || this === undefined)
            return ''

        const month:string = (this.getMonth() + 1).toString().padStart(2, '0')
        const strDate:string = this.getDate().toString().padStart(2, '0')
        const starHours:string = this.getHours().toString().padStart(2, '0')
        const starMinutes:string = this.getMinutes().toString().padStart(2, '0')
        const starSeconds:string = this.getSeconds().toString().padStart(2, '0')
        const result:string = `${this.getFullYear()}-${month}-${strDate} ${starHours}:${starMinutes}:${starSeconds}`
        return result
    }

    /**
     * 转换省略号字符串
     * @param maxLength 最大长度
     */
    String.prototype.ellipsis = function(maxLength:number) {
        return this.length > maxLength ? this.substring(0, maxLength) + '...' : this.toString()
    }

}